package com.example.wanjialei.service.serviceimpl;

import com.example.wanjialei.mapper.InformationMapper;
import com.example.wanjialei.pojo.Information;
import com.example.wanjialei.service.InformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InformationImpl implements InformationService {
    @Resource
    private InformationMapper informationMapper;

    @Override
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        List<Information> rows = informationMapper.selectByExample(null);
        if (rows.size() > 0) {
            map.put("flag", true);
            map.put("message", "查询成功！");
            map.put("total", rows.size());
            map.put("rows", rows);
        } else {
            map.put("flag", false);
            map.put("message", "查询失败！");
            map.put("total", rows.size());
            map.put("rows", rows);
        }
        return map;
    }


    @Override
    public Map<String, Object> modifyById(Information information) {
        Map<String, Object> map = new HashMap<>();
        int count = informationMapper.updateByPrimaryKeySelective(information);
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "修改成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "查询失败！");
            map.put("total", count);
        }
        return map;
    }

    @Override
    public Map<String, Object> insert(Information information) {
        Map<String, Object> map = new HashMap<>();
        int count = informationMapper.insert(information);
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "插入成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "插入失败！");
            map.put("total", count);
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteById(Information information) {
        Map<String, Object> map = new HashMap<>();
        int count = informationMapper.deleteByPrimaryKey(information.getId());
        if (count > 0) {
            map.put("flag", true);
            map.put("message", "删除成功！");
            map.put("total", count);
        } else {
            map.put("flag", false);
            map.put("message", "删除失败！");
            map.put("total", count);
        }
        return map;
    }
}
